package com.trade.common.utils.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * @描述：响应数据对象
 *
 */
public class ResultObject
{

	private HashMap<String, Object> datas = null;

	public ResultObject()
	{
		datas = new HashMap<String, Object>();
	}

	public ResultObject(String resultCode, String resultMsg)
	{
		datas = new HashMap<String, Object>();
		datas.put(Result.Key.RESULT_CODE, resultCode);
		datas.put(Result.Key.RESULT_MSG, resultMsg);
	}

	/** 增加响应数据 */
	public void addResult(String key, Object value)
	{
		datas.put(key, value);
	}

	/** 响应数据的JSON表示 */
	public String toString()
	{
		if (datas == null) return "";
		try
		{
			// Gson gson = new Gson();
			// return gson.toJson(datas);
			LowercaseNameFilter filter = new LowercaseNameFilter();
			return JSON.toJSONString(datas, filter, new SerializerFeature[0]);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 转成xml形式的数据
	 * 
	 * @return
	 */
	public String toXml()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		sb.append("<response>");
		Iterator<String> it = datas.keySet().iterator();
		while (it.hasNext())
		{
			String key = it.next();
			Object value = datas.get(key);
			toXml(sb, key, value);
		}
		sb.append("</response>");
		return sb.toString();
	}

	private void toXml(StringBuffer sb, String key, Object o)
	{
		if (o == null || key == null) return;
		if (o instanceof String || o instanceof Boolean || o instanceof Integer || o instanceof Double || o instanceof Double || o instanceof Float || o instanceof Long)
		{
			sb.append("<").append(key).append(">");
			sb.append(o);
			sb.append("</").append(key).append(">");
		}
		else if (o instanceof Date)
		{
			sb.append("<").append(key).append(">");
			sb.append(DateFormatUtils.format((Date) o, "yyyy-MM-dd HH:mm:ss"));
			sb.append("</").append(key).append(">");
		}
		else if (o instanceof List)
		{ // 处理List
			sb.append("<").append(key).append(">");
			List list = (List) o;
			for (Object obj : list)
			{
				toXml(sb, key, obj);
			}
			sb.append("</").append(key).append(">");
		}
		else
		{ // 处理Object
			Class cs = o.getClass();
			Field[] fields = cs.getDeclaredFields();
			Field.setAccessible(fields, true);
			key = cs.getSimpleName().toLowerCase();
			sb.append("<").append(key).append(">");
			for (Field field : fields)
			{
				try
				{
					String name = field.getName();
					Object value = PropertyUtils.getProperty(o, name);
					toXml(sb, name, value);
				}
				catch(Exception e)
				{}
			}
			sb.append("</").append(key).append(">");
		}
	}

	/**
	 * 获取响应数据
	 * 
	 * @return
	 */
	public HashMap<String, Object> getDatas()
	{
		return datas;
	}

	/** 清空响应数据 */
	public void clear()
	{
		if (datas != null)
		{
			datas.clear();
			datas = null;
		}
	}
}
